package edu.hawaii.duedates.webapp;

import static org.junit.Assert.assertNotNull;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
import edu.hawaii.duedates.datastore.DueDatesConfig;
import edu.hawaii.duedates.datastore.User;

/**
 * Test LoginPage class.
 * @author Daniel Arakaki, Jeho Jung, Arthur Shum, Daniel Tian
 *
 */
public class TestLoginPage {

  private DueDatesConfig config;
  
  /**
   * Create a test configuration.
   */
  @Before
  public void setUp() {
    this.config = new DueDatesConfig();
    User user = new User("name1", "email1@test.com", "password1");
    this.config.addUser(user);
    this.config.setSmtpServer("smtpserver1");
  }
  
  /**
   * Test LoginPage.
   */
  @Test
  public void testLogin() {
    WicketTester tester = new WicketTester(new DueDatesApplication(config));
    tester.startPage(LoginPage.class);
    
    tester.assertRenderedPage(LoginPage.class);
    assertNotNull("wicket ID form found.", tester.getTagByWicketId("form"));
    assertNotNull("wicket ID email found.", tester.getTagByWicketId("email"));
    assertNotNull("wicket ID password found.", tester.getTagByWicketId("password"));
    assertNotNull("wicket ID feedback found.", tester.getTagByWicketId("feedback"));
    tester.assertContains("Email:");

    // test bad login
    FormTester badtester = tester.newFormTester("form");
    badtester.setValue("email", "blah@blah.com");
    badtester.setValue("password", "blah");
    badtester.submit();
    tester.assertRenderedPage(LoginPage.class);
    tester.assertInfoMessages(new String[] { "Invalid login!" });
    
    // test good login
    tester.startPage(LoginPage.class);
    FormTester goodtester = tester.newFormTester("form");
    goodtester.setValue("email", "email1@test.com");
    goodtester.setValue("password", "password1");
    goodtester.submit();
    tester.assertRenderedPage(HomePage.class);

  }
}
